﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DatNuocViet.Models
{
    public class ToursDAO
    {
        public static List<Tour> GetFeaturedTours(int _pageindex, int _pagesize)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<LayDanhSachToursResult> listTour = data.LayDanhSachTours(_pageindex, _pagesize, null, null, 2, null).ToList();
                List<Tour> listTemp = new List<Tour>();
                if (listTour.Count < _pagesize)
                {
                    listTemp = data.Tours.Where(p => p.Featured == true && p.Deleted == false).OrderBy(p => p.CreateOn).Take(_pagesize - listTour.Count).ToList();
                }

                List<Tour> result = new List<Tour>();

                foreach (var item in listTour)
                {
                    Tour temp = new Tour();
                    temp.Id = item.Id;
                    temp.NameVn = item.NameVn;
                    temp.NameEn = item.NameEn;
                    temp.DescriptionVn = item.DescriptionVn;
                    temp.DescriptionEn = item.DescriptionEn;
                    temp.DetailsVn = item.DetailsVn;
                    temp.DetailsEn = item.DetailsEn;
                    temp.CreateOn = item.CreateOn;
                    temp.Price = item.Price;
                    temp.TourType = item.TourType;
                    temp.Location = item.Location;
                    temp.Deleted = item.Deleted;
                    temp.Featured = item.Featured;
                    //temp.Avatar = item.Avatar;

                    result.Add(temp);
                }
                foreach (var item in listTemp)
                {
                    result.Add(item);
                }

                return result;
            }
            catch
            {
                return new List<Tour>();
            }
        }

        public static int getAmountOfFeaturedTours()
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            return (data.DemDanhSachTour(null, 2, null) ?? 0);
        }

        public static List<Tour> GetListToursIndex(int _type)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<Tour> listTours = (from c in data.Tours
                                        where c.Deleted == false && c.onHomepage == true && c.TourType == _type
                                        select c).ToList();

                return listTours;
            }
            catch
            {
                return new List<Tour>();
            }
        }

        public static Tour GetDetailTour(int _id)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                TourDetailsResult tour = data.TourDetails(_id).FirstOrDefault();

                Tour temp = new Tour();
                temp.Id = tour.Id;
                temp.NameVn = tour.NameVn;
                temp.NameEn = tour.NameEn;
                temp.DescriptionVn = tour.DescriptionVn;
                temp.DescriptionEn = tour.DescriptionEn;
                temp.DetailsVn = tour.DetailsVn;
                temp.DetailsEn = tour.DetailsEn;
                temp.CreateOn = tour.CreateOn;
                temp.Price = tour.Price;
                temp.TourType = tour.TourType;
                temp.Location = tour.Location;
                temp.Featured = tour.Featured;

                return temp;
            }
            catch
            {
                return new Tour();
            }
        }

        public static int getAmountOfToursByType(int _type)
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            return (data.DemDanhSachTour(_type.ToString(), 1, null) ?? 0);
        }

        public static List<Tour> GetToursByType(int _pageindex, int _pagesize, int _type)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<LayDanhSachToursResult> listTour = data.LayDanhSachTours(_pageindex, _pagesize, null, _type.ToString(), 1, null).ToList();

                List<Tour> result = new List<Tour>();

                foreach (var item in listTour)
                {
                    Tour temp = new Tour();
                    temp.Id = item.Id;
                    temp.NameVn = item.NameVn;
                    temp.NameEn = item.NameEn;
                    temp.DescriptionVn = item.DescriptionVn;
                    temp.DescriptionEn = item.DescriptionEn;
                    temp.DetailsVn = item.DetailsVn;
                    temp.DetailsEn = item.DetailsEn;
                    temp.CreateOn = item.CreateOn;
                    temp.Price = item.Price;
                    temp.TourType = item.TourType;
                    temp.Location = item.Location;
                    temp.Deleted = item.Deleted;
                    temp.Featured = item.Featured;
                    //temp.Avatar = item.Avatar;

                    result.Add(temp);
                }

                return result;
            }
            catch
            {
                return new List<Tour>();
            }
        }

        public static List<GetTourTypeResult> getTourType()
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            return data.GetTourType().ToList();
        }

        public static TourType getTourType(int _type)
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

            TourType result = data.TourTypes.Single(p => p.Id == _type);

            return result;
        }

        public static List<Tour> SearchTours(string keyword, int pageIndex, int pageSize, int type, int min, int max, string lang)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                List<SearchTourResult> tempList = data.SearchTour(pageIndex, pageSize, keyword, min, max, type, "[Tours].[CreateOn]", "desc", lang, "", -1, -1).ToList();

                List<Tour> result = new List<Tour>();

                foreach (var item in tempList)
                {
                    Tour temp = new Tour();
                    temp.Id = item.TourId;
                    temp.NameVn = item.NameVn;
                    temp.NameEn = item.NameEn;
                    temp.DescriptionVn = item.DescriptionVn;
                    temp.DescriptionEn = item.DescriptionEn;
                    temp.DetailsVn = item.DetailsVn;
                    temp.DetailsEn = item.DetailsEn;
                    temp.Price = item.Price;
                    temp.TourType = item.TourTypeId;
                    temp.Location = item.Location;
                    //temp.Avatar = item.Avatar;

                    result.Add(temp);
                }

                return result;
            }
            catch
            {
                return new List<Tour>();
            }
        }

        public static int CountSearchTours(string keyword, int type, int min, int max, string lang)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                return data.SearchTour(1, -1, keyword, min, max, type, "[Tours].[CreateOn]", "desc", lang, "", -1, -1).Count();
            }
            catch
            {
                return 0;
            }
        }

        public static List<string> FindToursNames(string searchText, int maxResults, string lang)
        {
            DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();
            List<string> names = new List<string>();

            if(lang == "en")
            {
                names = (from n in data.Tours
                         where n.NameEn.Contains(searchText)
                         orderby n.NameEn
                         select n.NameEn).Take(maxResults).ToList();
            }
            else
            {
                names = (from n in data.Tours
                         where n.NameVn.Contains(searchText)
                         orderby n.NameVn
                         select n.NameVn).Take(maxResults).ToList();
            }
            return names;
        }

        public static string GetImageTours(int _id)
        {
            try
            {
                DatNuocVietDbDataContext data = new DatNuocVietDbDataContext();

                string result = data.GetToursImage(_id).First().Images;

                return result;
            }
            catch
            {
                return "/Resource/no_image.gif";
            }
        }
    }
}